from random import randint
import sys
import glob

class MarkovUnit:
    UnitCount = dict()
    Unit = ""
    NumberOfEncounters = 0
    def __init__(self, unit):
        self.Unit = unit
    def GetNext(self):
        index = randint(1, self.NumberOfEncounters)
        for x in self.UnitCount.iterkeys():
            if self.UnitCount[x] >= index:
                return x
            else:
                index = index - self.UnitCount[x]
    def AddNew(self,newUnit):
        if self.UnitCount.has_key(newUnit):
            self.UnitCount[newUnit] += 1
        else:
            self.UnitCount[newUnit] = 1
        self.NumberOfEncounters += 1

pathName = raw_input("Enter the path of a text file or a directory with text files in it to open: ")
jobType = raw_input("Enter the number of elements for each unit, or enter 0 to iterate over whole words: ")
if pathName.endswith(".txt"):
    fileHandle = open(pathName, "r")
elif pathName.count(".") != 0:
    print "Only text files (*.txt) are valid files."
    sys.exit()
else:
    if pathName.endswith("/"):
        onlyfiles = glob.glob(pathName + "*.txt")
    else:
        onlyfiles = glob.glob(pathName + "/*.txt")
